package com.icss.ah.shippingTemplate.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.icss.ah.base.bean.EasyuiDataGridJson;
import com.icss.j2ee.util.UUID;

/**
 * 
 * @author xuyl
 * 运费模板服务类
 * 1、运费模板信息列表查询
 * 2、运费模板状态修改
 * 3、删除运费模板信息
 * 4、运费模板信息保存
 * 5、运费模板包邮条件查询
 * 6、运费模板信息查询
 * 7、省市县查询
 * 8、发货时间查询
 *
 */
@Service
@Transactional
public class ShippingTemplateService {
	
	@Autowired
	private SqlSessionTemplate sqlSessionTemplate;
	
	private final Logger errlog = Logger.getLogger("E");
	private final Logger infolog = Logger.getLogger("I");
	
	private EasyuiDataGridJson dataGridJson = new EasyuiDataGridJson();
	
	/**
	 * 运费模板信息列表查询
	 * @param map
	 * @return
	 */
	public EasyuiDataGridJson queryShippingTemplateList(Map<String, String> map){
		try {
			List<Map<String, Object>> list = sqlSessionTemplate.selectList("ShippingTemplateBean.queryShippingTemplateList",map);
			Long total = sqlSessionTemplate.selectOne("ShippingTemplateBean.queryShippingTemplateListCount",map);
			infolog.info("ShippingTemplateService|queryShippingTemplateList|para:"+map+"|total:"+total);
			dataGridJson.setRows(list);
			dataGridJson.setTotal(total);
		} catch (Exception e) {
			errlog.error("ShippingTemplateService|queryShippingTemplateList",e);
		}
		return dataGridJson;
	}
	
	/**
	 * 运费模板状态修改
	 * @param map
	 * @return
	 */
	public String updateStatus(Map<String, String> map){
		String result =  "F";
		try {
			sqlSessionTemplate.update("ShippingTemplateBean.updateStatus",map);
			result = "T";
		} catch (Exception e) {
			errlog.error("ShippingTemplateService|updateStatus",e);
		}
		return result;
	}
	
	/**
	 * 删除运费模板信息
	 * @param map
	 * @return
	 */
	public String delInfo(Map<String, String> map){
		String result =  "F";
		try {
			sqlSessionTemplate.update("ShippingTemplateBean.delInfo",map);
			sqlSessionTemplate.delete("ShippingTemplateBean.deleteItemInfo",map);
			result = "T";
		} catch (Exception e) {
			errlog.error("ShippingTemplateService|updateStatus",e);
		}
		return result;
	}
	
	/**
	 * 运费模板信息保存
	 * @param map
	 */
	public String saveInfo(Map<String, String> map,List<Map<String, String>> list){
		String result =  "F";
		try {
			String flag = map.get("flag");
			if("update".equals(flag)){
				sqlSessionTemplate.update("ShippingTemplateBean.updateInfo",map);
			}else{
				sqlSessionTemplate.insert("ShippingTemplateBean.insertInfo",map);
			}
			//删除运费条件
			sqlSessionTemplate.delete("ShippingTemplateBean.deleteItemInfo",map);
			if(!list.isEmpty())
				sqlSessionTemplate.insert("ShippingTemplateBean.insertItemInfo",list);
			result = "T";
		} catch (Exception e) {
			errlog.error("ShippingTemplateService|saveInfo",e);
		}
		return result;
	}
	
	/**
	 * 运费模板包邮条件查询
	 * @param uuid
	 * @return
	 */
	public EasyuiDataGridJson queryItemList(Map<String, String> map){
		try {
			List<Map<String, Object>> list = sqlSessionTemplate.selectList("ShippingTemplateBean.queryItemList",map);
			Long total = sqlSessionTemplate.selectOne("ShippingTemplateBean.queryItemListCount",map);
			infolog.info("ShippingTemplateService|queryShippingTemplateList|para:"+map+"|total:"+total);
			dataGridJson.setRows(list);
			dataGridJson.setTotal(total);
		} catch (Exception e) {
			errlog.error("ShippingTemplateService|queryShippingTemplateList",e);
		}
		return dataGridJson;
	}
	
	/**
	 * 运费模板信息详情
	 * @param uuid
	 * @return
	 */
	public Map<String, Object> queryInfo(String uuid){
		Map<String, Object> bean = new HashMap<String, Object>();
		try {
			bean = sqlSessionTemplate.selectOne("ShippingTemplateBean.queryInfo",uuid);
		} catch (Exception e) {
			errlog.error("ShippingTemplateService|queryInfo",e);
		}
		return bean;
	}
	
	/**
	 * 省市县区查询
	 * @param map
	 * @return
	 */
	public List<Map<String, Object>> queryProvinceCityList(Map<String, String> map){
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		try {
			list = sqlSessionTemplate.selectList("ShippingTemplateBean.queryProvinceCityList",map);
			infolog.info("ShippingTemplateService|queryProvinceCityList|list:"+list.size());
		} catch (Exception e) {
			errlog.error("ShippingTemplateService|queryProvinceCityList", e);
		} 
		return list;
	}
	
	/**
	 * 发货时间字典查询
	 * @param map
	 * @return
	 */
	public List<Map<String, Object>> queryDeliveryTime(){
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		try {
			list = sqlSessionTemplate.selectList("ShippingTemplateBean.queryDeliveryTime");
			infolog.info("ShippingTemplateService|queryDeliveryTime|list:"+list.size());
		} catch (Exception e) {
			errlog.error("ShippingTemplateService|queryDeliveryTime", e);
		} 
		return list;
	}

}
